class Present::Link < Present::UIObject
  def initialize(label, url, options = {})
    options[:label] = label
    options[:url] = url
    options[:html] ||= {}
    super(options)
  end

  # the label can be a string (static) or a proc (dynamic) or a symbol (dynamic)
  # * string labels: always work, always the same.
  # * symbol labels: only work for per-record links, and specify a method on the record
  # * proc labels: always work, but for per-record links they must accept the record as the only argument
  attr_writer :label
  def label(record = nil)
    case @label
    when String
    @label
    when Symbol
    record.send(@label)
    when Proc
    record ? @label.call(record) : @label.call
    end
  end

  # the url (or the options to generate the url)
  attr_writer :url
  def url(record = nil)
    (record and url.is_a?(Hash)) ? @url.merge(:id => record) : @url
  end

  # HTTP Verb - GET, POST, PUT, DELETE
  attr_accessor :method

  # miscellaneous html attributes (e.g. class, target, etc.)
  attr_accessor :html

  def render(record = nil)
    link_to(self.label(record), self.url(record), html)
  end
end